package com.zte.cl.parser.utils;

import java.util.List;

import us.codecraft.webmagic.selector.CssSelector;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.JsonPathSelector;
import us.codecraft.webmagic.selector.PlainText;
import us.codecraft.webmagic.selector.RegexSelector;
import us.codecraft.webmagic.selector.Selectable;
import us.codecraft.webmagic.selector.Selector;
import us.codecraft.webmagic.selector.XpathSelector;

import com.zte.cl.entity.Expr;
import com.zte.cl.entity.Replace;
import com.zte.cl.utils.StringUtil;

public class SelectableUtil {
	
	public static Selector getSelector(String extractBy, String exprValue) {
		Selector selector;
		if (extractBy.equals("css")) {
			selector = new CssSelector(exprValue);
		} else if (extractBy.equals("regex")) {
			selector = new RegexSelector(exprValue);
		} else if (extractBy.equals("xpath")) {
			selector = getXpathSelector(exprValue);
		} else if (extractBy.equals("jsonpath")) {
			selector = new JsonPathSelector(exprValue);
		} else {
			selector = getXpathSelector(exprValue);
		}
		return selector;
	}
	
	public static Selector getSelector(String extractBy, String exprValue,Integer group) {
		if(group==null||group.intValue()==0){
			group=1;
		}
		Selector selector;
		if (extractBy.equals("css")) {
			selector = new CssSelector(exprValue);
		} else if (extractBy.equals("regex")) {
			selector = new RegexSelector(exprValue,group);
		} else if (extractBy.equals("xpath")) {
			selector = getXpathSelector(exprValue);
		} else if (extractBy.equals("jsonpath")) {
			selector = new JsonPathSelector(exprValue);
		} else {
			selector = getXpathSelector(exprValue);
		}
		return selector;
	}

	private static Selector getXpathSelector(String value) {
		Selector selector = new XpathSelector(value);
		return selector;
	}

	public static Selectable getSelectable(Html html, List<Expr> exprs) {
		Selectable selectable = null;
		for (Expr e : exprs) {
			Selector selector = getSelector(e.getExtractType().toString(), e.getExprValue(),e.getGroup());
			if (selectable == null) {
				selectable = html.selectList(selector);
			} else {
				selectable = selectable.selectList(selector);
			}
		}
		return selectable;
	}

	public static Selectable getSelectable(String rawText, List<Expr> exprs) {
		Selectable selectable = null;
		for (Expr e : exprs) {
			Selector selector = getSelector(e.getExtractType().toString(), e.getExprValue(),e.getGroup());
			if (selectable == null) {
				selectable =  PlainText.create(rawText).selectList(selector);
			} else {
				selectable = selectable.selectList(selector);
			}
		}
		return selectable;
	}

	public static String matchValue(String val, List<Expr> exprs) {
		if (StringUtil.isEmpty(val) || exprs == null || exprs.size() == 0) {
			return null;
		}
		Selectable listSt = SelectableUtil.getSelectable(val, exprs);
		List<String> listVal = listSt.all();
		if (listVal != null && listVal.size() > 0) {
			return listVal.get(0);
		}
		return null;
	}

	public static String replace(String content, List<Replace> replaces) {
		if (StringUtil.isEmpty(content) || replaces == null || replaces.size() == 0) {
			return content;
		}
		for (Replace r : replaces) {
			content = content.replaceAll(r.getRegex(), (r.getValue() == null ? "" : r.getValue()));
		}
		return content;
	}

}
